package com.cxrj.zhsq.userManage.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cxrj.zhsq.userManage.dao.OrganizationMapper;
import com.cxrj.zhsq.userManage.dao.UserMapper;
import com.cxrj.zhsq.userManage.entity.Organization;
import com.cxrj.zhsq.userManage.service.OrganizationService;

@Service
public class OrganizationServiceImpl implements OrganizationService {
	@Autowired
	private OrganizationMapper organizationMapper;

	@Resource
	private UserMapper userMapper;

	@Override
	public List<Map<String, Object>> selectOrganizationTree(Map<String, Object> map) {
		List<Map<String, Object>> organizations = organizationMapper.selectOrganizationByParentId(map);

		if (organizations.size() == 0) {
			return null;
		} else {
			for (Map<String, Object> m : organizations) {
				if (m.get("editTime") != null) {
					m.put("editTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) m.get("editTime")));
				}
				map.put("organizationId", m.get("organizationId"));
				List<Map<String, Object>> children = selectOrganizationTree(map);
				if (children != null) {
					m.put("children", children);
				}
			}
			return organizations;
		}
	}

	@Override
	public List<Map<String, Object>> selectOrganizationTreeNum(Map<String, Object> map) {
		List<Map<String, Object>> organizations = organizationMapper.selectOrganizationByParentId(map);

		if (organizations.size() == 0) {
			return null;
		} else {
			for (Map<String, Object> m : organizations) {
				if (m.get("editTime") != null) {
					m.put("editTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((Date) m.get("editTime")));
				}
				map.put("organizationId", m.get("organizationId"));
				int num = userMapper.getNumById((int) m.get("organizationId"));
				m.put("organizationName", m.get("organizationName") + "(" + num + ")");
				List<Map<String, Object>> children = selectOrganizationTreeNum(map);
				if (children != null) {
					m.put("children", children);
				}
			}
			return organizations;
		}
	}

	@Override
	public Map<String, Object> selectOrganizationCode(Map<String, Object> map) {
		Map<String, Object> organization = organizationMapper.selectOrganizationByCode(map);
		if (organization != null) {
			map.put("result", 1);
		} else {
			map.put("result", 0);
		}
		return map;
	}

	@Override
	public void insertOrganization(Organization organization) {
		organizationMapper.insertSelective(organization);
	}

	@Override
	public void updateOrganization(Map<String, Object> map) {
		organizationMapper.updateOrganization(map);
	}

	@Override
	public void deleteOrganization(Map<String, Object> map) {
		List<Object> ids = new ArrayList<Object>();
		ids.add(map.get("organizationId"));
		selectChildrenIds(map, ids);
		organizationMapper.deleteByIds(ids);
	}

	@Override
	public Map<String, Object> selectTopOrganization() {
		return organizationMapper.selectTopOrganization();
	}

	public void selectChildrenIds(Map<String, Object> map, List<Object> ids) {

		List<Map<String, Object>> list = organizationMapper.selectByParentId(map);

		for (Map<String, Object> m : list) {
			ids.add(m.get("organizationId"));
			map.put("organizationId", m.get("organizationId"));
			selectChildrenIds(map, ids);
		}
	}


	@Override
	public Map<String, Object> selectCommunityList(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		String cmod = request.getParameter("cmod");
		
		sqlMap.put("start", Integer.parseInt(start));
		if (cmod == null || cmod == "") {
			sqlMap.put("condition", " where  parent_id = 1 ");
			recordsTotal = organizationMapper.getCommunityListCount("parent_id = 1") + "";
		}else {
			sqlMap.put("condition", "  where parent_id = 1 and (organization_name like "+'"'+'%'+cmod+'%'+'"'+" or organization_principal like "+'"'+'%'+cmod+'%'+'"'+" or edit_account like "+'"'+'%'+cmod+'%'+'"'+")  ");
			recordsTotal = organizationMapper.getCommunityListCount( " parent_id = 1 and (organization_name like "+'"'+'%'+cmod+'%'+'"'+" or organization_principal like "+'"'+'%'+cmod+'%'+'"'+" or edit_account like "+'"'+'%'+cmod+'%'+'"'+" )") + "";
		}
		
		sqlMap.put("length", Integer.parseInt(length));
//		java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm");
		List<Map<String, Object>> list = organizationMapper.selectCommunityListPager(sqlMap);			
		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> selectAllCommunityList() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<Map<String, Object>> list = organizationMapper.selectAllCommunity();
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectAllCommunityByName(String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		sqlMap.put("name", name);
		resultMap.put("list", organizationMapper.selectByName(sqlMap));
		return resultMap;
	}

}
